package com.yuanlrc.hotel.service.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.service.domain.AboutService;
import com.yuanlrc.hotel.service.service.IAboutServiceService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

/**
 * 酒店服务Controller
 * 
 * @author yuanlrc
 * @date 2020-11-24
 */
@Controller
@RequestMapping("/hotel/service")
public class AboutServiceController extends BaseController
{
    private String prefix = "hotel/service";
     @Autowired
    private IAboutServiceService aboutServiceService;

    @RequiresPermissions("hotel:service:view")
    @GetMapping()
    public String service()
    {
        return prefix + "/service";
    }

    /**
     * 查询酒店服务列表
     */
    @RequiresPermissions("hotel:service:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AboutService aboutService)
    {
        startPage();
        List<AboutService> list = aboutServiceService.selectAboutServiceList(aboutService);
        return getDataTable(list);
    }

    /**
     * 导出酒店服务列表
     */
    @RequiresPermissions("hotel:service:export")
    @Log(title = "酒店服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AboutService aboutService)
    {
        List<AboutService> list = aboutServiceService.selectAboutServiceList(aboutService);
        ExcelUtil<AboutService> util = new ExcelUtil<AboutService>(AboutService.class);
        return util.exportExcel(list, "service");
    }

    /**
     * 新增酒店服务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存酒店服务
     */
    @RequiresPermissions("hotel:service:add")
    @Log(title = "酒店服务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AboutService aboutService)
    {
        if (aboutServiceService.selectAboutServiceByServiceName(aboutService.getService())!=null){
            return error("酒店服务项名不能重复!");
        }
        if (aboutService.getService()==null||aboutService.getService().length()==0||aboutService.getService()==" "){
            return error("酒店服务项名不能为空");
        }else if (aboutService.getDetail().length()<10){
            return error("酒店服务项详情过短");
        }
        int count = 0;
        try{
            count = aboutServiceService.insertAboutService(aboutService);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 修改酒店服务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AboutService aboutService = aboutServiceService.selectAboutServiceById(id);
        mmap.put("aboutService", aboutService);
        return prefix + "/edit";
    }

    /**
     * 修改保存酒店服务
     */
    @RequiresPermissions("hotel:service:edit")
    @Log(title = "酒店服务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AboutService aboutService)
    {
        if (aboutService.getService()==null||aboutService.getService().length()==0||aboutService.getService()==" "){
            return error("酒店服务项名不能为空");
        }else if (aboutService.getDetail().length()<10){
            return error("酒店服务项详情过短");
        }
        int count = 0;
        try{
            count = aboutServiceService.updateAboutService(aboutService);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 删除酒店服务
     */
    @RequiresPermissions("hotel:service:remove")
    @Log(title = "酒店服务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int count = 0;
        try {
            count = aboutServiceService.deleteAboutServiceByIds(ids);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }
}
